package com.fire.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fire.bo.supplier.req.ProductReq;
import com.fire.bo.supplier.req.ProductStatusReq;
import com.fire.bo.supplier.req.SupplierListReq;
import com.fire.bo.supplier.req.SupplierReq;
import com.fire.bo.supplier.res.ProductRes;
import com.fire.bo.supplier.res.SupplierListRes;
import com.fire.common.RspObjBean;
import com.fire.service.SupplierService;
import com.fire.util.BussinessUtils;
import com.fire.util.ErrRsp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@CrossOrigin
@RequestMapping("/pc/supplier")
@Api(value = "供应商管理", description = "供应商管理")
public class SupplierController
{
	@Autowired
	private SupplierService supplierService;
	
	/**
	 * 供应商审批
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "供应商审批", notes = "供应商审批")
	@RequestMapping(value = "/updateSupplierReq", method = RequestMethod.POST)
	public @ResponseBody RspObjBean update(
			@RequestBody @Valid SupplierReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			supplierService.updateSupplierReq(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	/**
	 * 供应商列表
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "供应商列表", notes = "供应商列表",response = SupplierListRes.class)
	@RequestMapping(value = "/supplierList", method = RequestMethod.POST)
	public @ResponseBody RspObjBean SupplierList(
			@RequestBody @Valid SupplierListReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}
		
		try
		{
			List<SupplierListRes> list = supplierService.supplierList(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, list,supplierService.supplierListCount(bean));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	/**
	 * 商品列表
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "商品列表", notes = "商品列表",response = ProductRes.class)
	@RequestMapping(value = "/productList", method = RequestMethod.POST)
	public @ResponseBody RspObjBean productList(
			@RequestBody @Valid ProductReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}
		
		try
		{
			List<ProductRes> list = supplierService.productList(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, list,supplierService.productListCount(bean));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	/**
	 * 商品审核
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "商品审核", notes = "商品审核",response = ProductRes.class)
	@RequestMapping(value = "/updateProductReq",method = RequestMethod.POST)
	public @ResponseBody RspObjBean updateProductReq(
			@RequestBody @Valid ProductStatusReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}
		
		try
		{
			supplierService.updateProductReq(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
}
